$(function () {
    $("#user_group").change(function () {
        //获取日报
        var selected_report = $('#user_group').children('option:selected').val();
        //获取系统当前时间
        var CurrentDate = new Date();
        var year = CurrentDate.getFullYear();
        var Startime = year + "-1-1";
        var endtime = year + 1 + "-1-1";
        console.log(Startime);
        console.log(endtime);
        //ajax请求获取
        $.ajax(
            {
                url:"/report/getReportByFlag",
                type:"POST",
                dataType:"json",
                async:false,
                data:{flag:selected_report,startime:Startime,endtime:endtime},
                success:function (result) {
                    //console.log(result);
                        readReport(result);
                },
                error: function (xhr, status, p3, p4) {
                    var err = "Error " + " " + status + " " + p3;
                    if (xhr.responseText && xhr.responseText[0] == "{")
                        err = JSON.parse(xhr.responseText).message;
                    console.log(err);
                }
            });
    })
})
$(function () {
    //获取选中的地区ID
    $("#InputDomain1").change(function () {
        var selected_domain = $('#InputDomain1').children('option:selected').val();
        getBridgeByID(selected_domain);
    })
    //触发按钮提交页面
    $("#submitReport").click(function () {
        $("#submitSpecialReport").attr("action","/report/addSpecialReport").submit();
    })
})
function getBridgeByID(ID) {
    //ajax获取所有当前地区的桥梁
    $.getJSON("/bridge/domainbridge/"+ID,function (result) {
        readBridge(result);
    })
}
function readBridge(JsonResult) {
    //console.log(JsonResult);
    var addBridgeHtml = "<option value='-1'>请选择桥梁</option>";
    $.each(JsonResult,function (i,item) {
        addBridgeHtml += "<option value='"+ item[0] +"'>"+ item[4]+"</option>"
    })
    //console.log(addBridgeHtml);
    $("#InputBridge1").html(addBridgeHtml);
}
function readReport(JsonResult) {
    var ResultHtml = "";
    $.each(JsonResult,function (i,item) {
        ResultHtml += "<tr><td class='text-md-center yc'>"+ item["reportDate"] +"</td><td class='text-md-center'><a class=\"badge badge-info\" href=\"\" data-toggle=\"modal\" data-target=\"#ViewReport\">查看</a></td></tr>";
    });
    $("#ReportList").html(ResultHtml);
}

//使用事件冒泡解决js插入页面无法获取事件的问题
$("#ReportList").on("click",".badge",function () {
    var clickedDate = $(this).parents("tr").find(".yc").text();
    //console.log(clickedDate);
    var clicked = new Date(clickedDate);
    var clickedDateFormat = clicked.getFullYear() + "-" + (clicked.getMonth()+1) + "-" + clicked.getDate();
    var nextDay = clicked.getFullYear() + "-" + (clicked.getMonth()+1) + "-" + (clicked.getDate()+1);
    //获取选择的报表类型
    var selected_report = $("#user_group").children("option:selected").val();
    console.log(selected_report);
    //ajax进行请求
    $.ajax(
        {
            url:"/report/getReportByFlag",
            type:"POST",
            dataType:"json",
            async:false,
            data:{flag:selected_report,startime:clickedDateFormat,endtime:nextDay},
            success:function (result) {
                console.log(result);
                var viewReportHtml = "";
                $.each(result,function (i,item) {
                    //通过ajax再使用桥ID来请求桥的信息
                    var bridgeName = "";
                    //console.log(item["bridgeID"]);
                    $.ajaxSettings.async = false;//设置ajax同步提交
                    $.getJSON("/bridge/viewBridgeByID/"+ item["bridgeID"],function (Secondresult) {
                        //console.log(Secondresult);
                        bridgeName = Secondresult["name"];
                    })
                    var AllSenors = "";
                    $.ajaxSettings.async = false;//设置ajax同步提交
                    $.getJSON("/bridge/bridgesensor/" + item["bridgeID"],function (SensorResult) {
                        $.each(SensorResult,function (i,item) {
                            AllSenors += item["cdName"];
                        })
                    })
                    //console.log(bridgeName);
                    var reporCatergory = ((item["dataFlag"]== 1)? "系统日报":"特殊日报"  );
                    //拼接字符串
                    viewReportHtml += "<tr>\n" +
                        "                        <th scope=\"col\" class=\"text-md-center\">"+bridgeName +"</th>\n" +
                        "                        <th scope=\"col\" class=\"text-md-center\">"+item["dataTotalSum"]+"</th>\n" +
                        "                        <th scope=\"col\" class=\"text-md-center\">"+item["warningSum"]+"</th>\n" +
                        "                        <th scope=\"col\" class=\"text-md-center\">"+item["firstWarningSum"]+"</th>\n" +
                        "                        <th scope=\"col\" class=\"text-md-center\">"+item["secondWarningSum"]+"</th>\n" +
                        "                        <th scope=\"col\" class=\"text-md-center\">"+reporCatergory+"</th>\n" +
                        "                        <th scope=\"col\" class=\"text-md-center\">"+AllSenors+"</th>\n" +
                        "                        <th scope=\"col\" class=\"text-md-center\">"+item["reportType"]+"</th>\n" +
                        "                        <th scope=\"col\" class=\"text-md-center\"><a href='"+item["dataFile"]+"' class='btn btn-primary'>下载</a></th>\n" +
                        "                    </tr>"
                });
                $("#ShowReport").html(viewReportHtml);
            },
            error: function (xhr, status, p3, p4) {
                var err = "Error " + " " + status + " " + p3;
                if (xhr.responseText && xhr.responseText[0] == "{")
                    err = JSON.parse(xhr.responseText).message;
                console.log(err);
            }
        });
})
